package com.lankystar.logistucs.spring.dao.impl;


import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.logistucs.spring.dao.IDeleteDao;
import com.lankystar.logistucs.spring.entity.OneOrderAndOneStatus;
import com.lankystar.logistucs.spring.entity.OneStatus;

@Repository
public class DeleteDaoImpl implements IDeleteDao{

	@Autowired
	private SessionFactory sf;
	
	public Session getSession(){
		return sf.getCurrentSession();
	} 
	@SuppressWarnings("unchecked")
	@Override
	public int deleteOrder(int oneOrderId) {
		String hql ="select oao from OneOrderAndOneStatus oao join oao.oneOrder one where one.oneOrderId = "+oneOrderId+"";
		List<OneOrderAndOneStatus> oaos = getSession().createQuery(hql).list();
		Object[] obs = new  Object[oaos.size()];
		/**
		 * 先找出OneOrderAndOneStatus表中符号要求的数据，再循环删除
		 */
		int i = 0;
		for (OneOrderAndOneStatus oao : oaos) {
			OneStatus os = oao.getOneStatus();
			if(os.getOneStatusKey()!=30){
				obs[i] = oao;
				i++;
			}
		}
		if(obs!=null){
			for (int j = 0; j < obs.length; j++) {
				getSession().delete(obs[j]);
			}
		}
		return i;
	}

}
